"""
Regulatory Standards Database
Contains common regulatory documents and their official sources
Used when no relevant information is found in the knowledge base
"""

REGULATORY_STANDARDS = {
    'biocompatibility': [
        {
            'name': 'ISO 10993-1:2018',
            'title': 'Biological evaluation of medical devices - Part 1: Evaluation and testing within a risk management process',
            'link': 'https://www.iso.org/standard/68936.html',
            'authority': 'ISO'
        },
        {
            'name': 'FDA Biocompatibility Guidance',
            'title': 'Use of International Standard ISO 10993-1, "Biological evaluation of medical devices - Part 1: Evaluation and testing within a risk management process"',
            'link': 'https://www.fda.gov/regulatory-information/search-fda-guidance-documents/use-international-standard-iso-10993-1-biological-evaluation-medical-devices-part-1-evaluation-and',
            'authority': 'FDA'
        }
    ],
    'quality_management': [
        {
            'name': 'ISO 13485:2016',
            'title': 'Medical devices - Quality management systems - Requirements for regulatory purposes',
            'link': 'https://www.iso.org/standard/59752.html',
            'authority': 'ISO'
        },
        {
            'name': '21 CFR Part 820',
            'title': 'Quality System Regulation (QSR)',
            'link': 'https://www.accessdata.fda.gov/scripts/cdrh/cfdocs/cfcfr/CFRSearch.cfm?CFRPart=820',
            'authority': 'FDA'
        }
    ],
    'risk_management': [
        {
            'name': 'ISO 14971:2019',
            'title': 'Medical devices - Application of risk management to medical devices',
            'link': 'https://www.iso.org/standard/72704.html',
            'authority': 'ISO'
        }
    ],
    '510k': [
        {
            'name': 'FDA 510(k) Guidance',
            'title': 'The 510(k) Program: Evaluating Substantial Equivalence in Premarket Notifications [510(k)]',
            'link': 'https://www.fda.gov/regulatory-information/search-fda-guidance-documents/510k-program-evaluating-substantial-equivalence-premarket-notifications-510k',
            'authority': 'FDA'
        }
    ],
    'clinical_evaluation': [
        {
            'name': 'MEDDEV 2.7/1 Rev 4',
            'title': 'Clinical Evaluation: A Guide for Manufacturers and Notified Bodies',
            'link': 'https://ec.europa.eu/health/md_sector/new_regulations/guidance_en',
            'authority': 'EU MDR'
        }
    ],
    'nmpa_china': [
        {
            'name': 'NMPA Registration Requirements',
            'title': 'Medical Device Registration and Filing Regulations',
            'link': 'https://www.nmpa.gov.cn/xxgk/fgwj/gzwj/gzwjylqx/index.html',
            'authority': 'NMPA'
        }
    ]
}

REGULATORY_AUTHORITIES = {
    'FDA': {
        'name': 'U.S. Food and Drug Administration',
        'country': 'USA',
        'website': 'https://www.fda.gov/medical-devices',
        'description': 'Regulations, guidance documents, and submission requirements for medical devices in the United States'
    },
    'NMPA': {
        'name': 'National Medical Products Administration',
        'country': 'China',
        'website': 'https://www.nmpa.gov.cn',
        'description': 'Registration requirements and technical guidelines for medical devices in China'
    },
    'PMDA': {
        'name': 'Pharmaceuticals and Medical Devices Agency',
        'country': 'Japan',
        'website': 'https://www.pmda.go.jp/english/',
        'description': 'Approval and compliance documentation for medical devices in Japan'
    },
    'ANVISA': {
        'name': 'Brazilian Health Regulatory Agency',
        'country': 'Brazil',
        'website': 'https://www.gov.br/anvisa/en',
        'description': 'Registration materials and regulations for medical devices in Brazil'
    },
    'ISO': {
        'name': 'International Organization for Standardization',
        'country': 'International',
        'website': 'https://www.iso.org',
        'description': 'International standards for medical device quality, safety, and effectiveness'
    },
    'RAPS': {
        'name': 'Regulatory Affairs Professional Society',
        'country': 'International',
        'website': 'https://www.raps.org',
        'description': 'Regulatory resources, guidance, and professional development'
    }
}

def get_relevant_standards(query_keywords):
    """
    Get relevant regulatory standards based on query keywords
    """
    relevant = []
    query_lower = query_keywords.lower()
    
    for category, standards in REGULATORY_STANDARDS.items():
        if any(keyword in query_lower for keyword in category.split('_')):
            relevant.extend(standards)
    
    # If no specific match, return general quality and risk standards
    if not relevant:
        relevant = REGULATORY_STANDARDS.get('quality_management', [])[:1] + \
                   REGULATORY_STANDARDS.get('risk_management', [])[:1]
    
    return relevant[:3]  # Return top 3 relevant standards

def format_standards_response(standards):
    """
    Format standards list for response
    """
    response = "Not found in this knowledge base, but you can refer to the following information:\n\n"
    
    for i, std in enumerate(standards, 1):
        response += f"{i}. **{std['name']}** - {std['title']}\n"
        response += f"   Authority: {std['authority']}\n"
        response += f"   Link: {std['link']}\n\n"
    
    return response